﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewLife.CommonEntity;
using NewLife.Web;
using XControl;
using BingZi.HY.Core.Entities.Center;
using NewLife.CommonEntity.Web;
using BingZi.HY.Core.BLL;
using BingZi.HY.Core.Enums;


namespace BingZi.HY.UI.Admin.Manager
{
    public partial class Pages_app_InfoTypeForm : PageBase
    {
        /// <summary>编号</summary>
        public Int32 EntityID { get { return WebHelper.RequestInt(app_InfoType._.IT_ID); } }

        private app_InfoType _Entity;
        /// <summary>信息类型</summary>
        public app_InfoType Entity
        {
            get { return _Entity ?? (_Entity = app_InfoType.FindByKeyForEdit(EntityID)); }
            set { _Entity = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                //绑定选择的枚举
                BingZi.HY.Core.WebCommon.WebHelper.BindList(frmState, typeof(EnumState));
                if (EntityID > 0)
                    BingZi.HY.Core.WebCommon.WebHelper.SelectedListByValue(frmState, Entity.State);
                else
                    BingZi.HY.Core.WebCommon.WebHelper.SelectedListByValue(frmState, (int)EnumState.Normal);

                DataBind();


                // 添加/编辑 按钮需要添加/编辑权限
                if (EntityID > 0)
                    UpdateButton.Visible = Acquire(PermissionFlags.Edit);
                else
                    UpdateButton.Visible = Acquire(PermissionFlags.New);
            }
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            // 添加/编辑 按钮需要添加/编辑权限
            if (EntityID > 0 && !Acquire(PermissionFlags.Edit))
            {
                WebHelper.Alert("没有编辑权限！");
                return;
            }
            if (EntityID <= 0 && !Acquire(PermissionFlags.New))
            {
                WebHelper.Alert("没有添加权限！");
                return;
            }

            if (!WebHelper.CheckEmptyAndFocus(frmName, "必须填写信息类型！")) return;

            if (EntityID > 0)
            {
                int NameCount = app_InfoType.FindCount(app_InfoType._.Name, frmName.Text);
                if (NameCount > 1)
                {
                    WebHelper.AlertAndRefresh("信息类型已存在！");
                    return;
                }
                if (NameCount == 1)
                {
                    int keyid = app_InfoType.Find(app_InfoType._.Name, frmName.Text).IT_ID;
                    if (keyid != EntityID)
                    {
                        WebHelper.AlertAndRefresh("信息类型已存在！");
                        return;
                    }
                }
            }



            Entity.Name = frmName.Text;
            Entity.Description = frmDescription.Text;
            Entity.ShowOrder = int.Parse(frmShowOrder.Text);
            Entity.State = NewLife.Web.WebHelper.RequestInt(frmState.UniqueID);

            try
            {
                Entity.Save();
                //WebHelper.AlertAndRedirect("成功！", "Admin.aspx");
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('成功！');parent.Dialog.CloseAndRefresh(frameElement);", true);
            }
            catch (Exception ex)
            {
                WebHelper.Alert("失败！" + ex.Message);
            }
        }
    }
}